package com.shq.ex1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.sql.Time;
import java.util.Calendar;
import java.util.Date;

public class DateAndTimeSelActivity extends AppCompatActivity {

    private int year = -1;
    private int month = -1;
    private int day = -1;
    private int hour = -1;
    private int minute = -1;

    private TextView text = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_and_time_sel);

        DatePicker date = (DatePicker) findViewById(R.id.datePicker);//日期选择器控件
        TimePicker time = (TimePicker) findViewById(R.id.timePicker);//时间选择器控件

        text = (TextView) findViewById(R.id.textView);

        time.setIs24HourView(true);
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);

        date.init(year, month, day, new dateOnDateChangetdListener());

        time.setOnTimeChangedListener(new timeOnTimeChangedListener());

        text.setText("现在是：" + year + "年" + (month + 1) + "月" + day + "日" + hour + "时" + minute + "分");
    }

        class dateOnDateChangetdListener implements DatePicker.OnDateChangedListener {
            public void onDateChanged(DatePicker view,int year,int monthOfYear,int dayOfMonth){
                DateAndTimeSelActivity.this.year = year;
                DateAndTimeSelActivity.this.month = monthOfYear;
                DateAndTimeSelActivity.this.day = dayOfMonth;
                show(year,month,day,hour,minute);
            }
        }
        class timeOnTimeChangedListener implements TimePicker.OnTimeChangedListener {
            public void onTimeChanged(TimePicker view,int hourOfDay,int minute) {
                DateAndTimeSelActivity.this.hour = hourOfDay;
                DateAndTimeSelActivity.this.minute = minute;
                show(year, month, day, hour, minute);
            }
        }

        private void show(int year,int month,int day,int hour,int minute){
        text.setText("现在是："+year+"年"+(month+1)+"月"+day+"日"+hour+"时"+minute+"分");
    }
}
